package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.entity.ShopGoodsEntity;
import com.dragee.modules.work.service.ShopGoodsService;
import com.dragee.modules.work.service.ShopService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 用品表
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-03-29 14:41:10
 */
@RestController
@RequestMapping("work/shopgoods")
@Api(tags = "work - 用品表")
public class ShopGoodsController {
    @Autowired
    private ShopGoodsService shopGoodsService;
    @Autowired
    private ShopService shopService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("用品表列表")
    public R list(@RequestParam Map<String, Object> params){
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        params.put("shopId", shop == null ? "0" : shop.getId());
        PageUtils page = shopGoodsService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("用品表信息")
    public R info(@PathVariable("id") Long id){
		ShopGoodsEntity shopGoods = shopGoodsService.getById(id);
        return R.ok().put("data", shopGoods);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改用品表")
    public R saveOrUpdate(@RequestBody ShopGoodsEntity shopGoods){
        ShopEntity shop =
                shopService.getShopByUserId(LoginUserUtil.getLoginUserId() + "");
        if (shop == null) {
            return R.error("请先添加商铺");
        }
        shopGoodsService.saveOrUpdate(shopGoods);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("用品表删除")
    public R delete( String id){
		shopGoodsService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("用品表批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> shopGoodsService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
